import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import Mock from 'mockjs';

export class Hero {
  constructor(public id: number, public name: string, public type: string) { }
}

const HEROES: Hero[] = [
  new Hero(11, '诸葛亮', '法师'),
  new Hero(12, '后羿', '射手'),
  new Hero(13, '项羽', '坦克'),
  new Hero(14, '岳飞', '战士'),
];

Mock.mock(/heros/, {
  'heros': HEROES
})

const options = [
  { value: 1, label: '法师' },
  { value: 2, label: '射手' },
  { value: 3, label: '坦克', },
  { value: 4, label: '战士', }
];

@Injectable()
export class HeroService {

  constructor(
    private http: HttpClient,
  ) {
  }

  getHeroes() {
    return new Promise<Hero[]>(resolve => {
      this.http.get('heros').subscribe(
        (res: any) => {
          resolve(res.heros);
        }
        // err => {
        //   console.log('Something went wrong!');
        // }
      );
    });

    // return new Promise<Hero[]>(resolve => {
    //   setTimeout(() => { resolve(HEROES); }, 500);
    // });
  }

  getHero(id: number | string) {
    return this.getHeroes()
      .then(heroes => heroes.find(hero => hero.id === id));
  }

  getOptions() {
    return options;
  }

  getOptionLabel(value) {
    return options.find(obj => obj.value === value).label;
  }

  getOptionValue(label) {
    return options.find(obj => obj.label === label).value;
  }



}
